package com.example.mobile2021606127cxz0602homework;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText edtName;
    private EditText edtPsw;
    private CheckBox cbRmb;
    private CheckBox cbAuto;
    private Button btnLogin;
    private Button btnRegister;
    private SharedPreferences sp;
    private AlertDialog alertDialog;
    private boolean remember, autologin;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        btnLogin.setOnClickListener(this);
        btnRegister.setOnClickListener(this);
        sp = getSharedPreferences("login", Context.MODE_PRIVATE);

        /**
         *回显数据-第二次打开界面，从SP加载数据，进行同步
         */
        remember = sp.getBoolean("remember", false);
        autologin = sp.getBoolean("autologin", false);
        //记住密码，之前已经勾上
        if (remember) {
            //获取SP里面的name和psw,并保存到EditText
            String name = sp.getString("name", "");
            String psw = sp.getString("psw", "");
            edtName.setText(name);
            edtPsw.setText(psw);
            cbRmb.setChecked(true);//打上勾
        }
        //自动登录  之前已经勾上
        if (autologin) {
            cbAuto.setChecked(true); //打上勾
            Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();

        }
    }

    private void initView() {
        edtName = (EditText) findViewById(R.id.edt_name);
        edtPsw = (EditText) findViewById(R.id.edt_psw);
        cbRmb = (CheckBox) findViewById(R.id.cb_rmb);
        cbAuto = (CheckBox) findViewById(R.id.cb_auto);
        btnLogin = (Button) findViewById(R.id.btn_login);
        btnRegister = (Button) findViewById(R.id.btn_register);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                String name = edtName.getText().toString().trim();
                String psw = edtPsw.getText().toString().trim();
                if (TextUtils.isEmpty(name) || TextUtils.isEmpty(psw)) {
                    Toast.makeText(this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
                } else if (name.equals("admin") && psw.equals("123456")) {
                    Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                    /**
                     * 判断多选框-记住密码是否打勾
                     */
                    if (cbRmb.isChecked()) {
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString("name", name);
                        editor.putString("psw", psw);
                        editor.putBoolean("remember", true);
                        editor.apply();
                    }
                    /**
                     * 判断多选框-自动登录是否打勾
                     */
                    if (cbAuto.isChecked()) {
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putBoolean("autologin", true);
                        editor.apply();
                    }
                    /**
                     * 登录成功跳转到第二个Activity，并把数据回传
                     */
                    Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                    //第一种方式
                    intent.putExtra("userName", edtName.getText().toString().trim());
                    //第二种方式
                    Bundle bundle = new Bundle();
                    bundle.putString("passWord", edtPsw.getText().toString().trim());
                    intent.putExtras(bundle);
                    startActivity(intent);
                } else {
                    AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                    dialog.setMessage("密码或用户名错误!!");
                    dialog.setPositiveButton("确定", (dialog1, which) -> {

                    });
                    alertDialog = dialog.create();
                    alertDialog.show();
                }
                break;
            case R.id.btn_register:
                break;
        }
    }


}